﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wow_backitupSDK;
using System.Runtime;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
//using System.Timers;

namespace WoW_Watcher
{
    public class Core:GeneralPluginSDK
    {
        Process[] arproc = null;
        const string wowfilename = "Wow.exe";
        const string wowappname = "Wow";
        Process wowproc= new Process ();
        Timer wowtimer = new Timer();
       
        wow_backtiup.frmGui tform;
        bool wowopened = false;
        bool wowclosed = false;
       
       // System.Windows.Forms.ToolStripStatusLabel status = new System.Windows.Forms.ToolStripStatusLabel();
        public override string Name
        {
            get { return "WoW watcher"; }
        }
        public override void MakeAction(PluginContent plugcontent, wow_backtiup.frmGui tForm)
        {
           // if((tForm !=null )||(plugcontent !=null ))
            {
                tform = tForm;
                wowtimer.Interval=1000;
                wowtimer.Tick+= new EventHandler(wowtimer_tick);
                wowtimer.Start();
             //   tForm.btnBackup.Click+=  new EventHandler(btnbackup_click2);

               
            }
        }

        public override string GetPluginVersion()
        {
            string Ekdo = null;


            
            //Ekdo = Assembly.GetExecutingAssembly().GetName().Version.ToString();


            Ekdo = this.GetType().Assembly.GetName().Version.ToString();
            return Ekdo;
        }
        public override void Dispose()
        {
            
        }
       void exitedevent(object send, EventArgs ev)
        {
            //timer.Stop();
          //  if((wowclosed==true )||(wowopened!=true ))
            {
                wowtimer.Stop();
                //timer.Stop();
           //    tform.btnBackup_Click(send, ev);
               //timer.Start();
               wowtimer.Stop();
            }
            
        } 
       void wowtimer_tick(object send, EventArgs  ev)
        {
            try
            {
                arproc = Process.GetProcesses();
                if (arproc != null)
                {
                    foreach (Process proc in arproc)
                    {
                       

                        //      System.Console.WriteLine(arproc.Count().ToString());
                        if ((proc.ProcessName.Contains(wowappname) != false)||
                            (proc.ProcessName.Contains(wowfilename)!=false )
                            || (proc.ProcessName.Contains("World of Warcraft")!=false )) 
                        {
                           // System.Windows.Forms.MessageBox.Show(proc.ProcessName + "\n" + proc.ToString());
                            tform.statusInfo.Text = "World of Warcraft is open";

                            proc.EnableRaisingEvents = true;
                            proc.Exited+= new EventHandler(this.exitedevent);
                            break;

                        }

                    }
                }
              //  MessageBox.Show(tform.statusInfo.Text);
              
                
            }


            catch (Exception ex)
            {


            }

        }
       void btnbackup_click2(object send, EventArgs ev)
       {
           arproc = Process.GetProcesses();
           if (arproc != null)
           {
               foreach (Process proc in arproc)
               {


                   //      System.Console.WriteLine(arproc.Count().ToString());
                   if ((proc.ProcessName.Contains(wowappname) != false) ||
                       (proc.ProcessName.Contains(wowfilename) != false)
                       || (proc.ProcessName.Contains("World of Warcraft") != false))
                   {
                       // System.Windows.Forms.MessageBox.Show(proc.ProcessName + "\n" + proc.ToString());
                       tform.statusInfo.Text = "World of Warcraft is open";

                       proc.EnableRaisingEvents = true;
                       proc.Kill();
                       //   proc.Exited+= new EventHandler(this.exitedevent);
                       break;

                   }

               }

           }
           base.btnBackup_Click(send, ev);
       }
            }


        }
        
    

